﻿#region [ License information          ]
/* ************************************************************
 *
 * Copyright (c) Bjørn Bouet Smith, 2012
 *
 * This source code is subject to terms and conditions of 
 * Microsoft Public License (Ms-PL).
 * 
 * A copy of the license can be found in the license.txt
 * file at the root of this distribution. If you can not 
 * locate the License, please send an email to bjornsmith@gmail.com
 * 
 * By using this source code in any fashion, you are 
 * agreeing to be bound by the terms of the Microsoft 
 * Public License.
 *
 * You must not remove this notice, or any other, from this
 * software.
 *
 * ************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;

using System.Text;
using NUnit.Framework;

namespace Restful.Net.Tests
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    [TestFixture]
    public class ContentTypeNegotiatorTest
    {
        private ContentTypeNegotiator neg = new ContentTypeNegotiator();

        [Test]
        public void ShouldCorrectlySelectFirstWhenNoQualityOrLevelIsDefined()
        {
            string[] headers = new string[] { "text/plain", "text/*" };

            Accept accept = neg.GetRequestedContentType(headers);

            Assert.That(accept.MediaType, Is.EqualTo("text/plain"));
        }

        [Test]
        public void ShouldCorrectlySelectTheOneWithHighestImplicitQuality()
        {
            string[] headers = new[] { "text/plain", "text/*;q=0.6" };

            Accept accept = neg.GetRequestedContentType(headers);

            Assert.That(accept.MediaType, Is.EqualTo("text/plain"));
        }

        [Test]
        public void ShouldCorrectlySelectTheOneWithHighestExplicitQuality()
        {
            string[] headers = new[] { "text/plain;q=0.8", "text/*;q=0.6" };

            Accept accept = neg.GetRequestedContentType(headers);

            Assert.That(accept.MediaType, Is.EqualTo("text/plain"));
        }

        [Test]
        public void ShouldCorrectlySelectTheOneWithHighestExplicitLevel()
        {
            string[] headers = new[] { "text/plain;level=1", "text/*;q=0.6" };

            Accept accept = neg.GetRequestedContentType(headers);

            Assert.That(accept.MediaType, Is.EqualTo("text/plain"));
        }

        [Test]
        public void WildCardShouldBeTreatedLowerThanExplitsWithoutQualityDefined()
        {
            string[] headers = new[] { "text/*", "text/plain" };

            Accept accept = neg.GetRequestedContentType(headers);

            Assert.That(accept.MediaType, Is.EqualTo("text/plain"));
        }

        [Test]
        public void WildCardShouldBeTreatedLowerThanExplitsEvenWithQualityDefined()
        {
            string[] headers = new[] { "text/*;q=0.4", "text/plain;q=0.4" };

            Accept accept = neg.GetRequestedContentType(headers);

            Assert.That(accept.MediaType, Is.EqualTo("text/plain"));
        }
    }
}
